import math
import pyglet
from pyglet.window import key

#glEnable(GL_BLEND)
#glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

# Set Size of Window
WinWidth = 640
WinHeight = 480
window = pyglet.window.Window(WinWidth, WinHeight)

GFXbatch = pyglet.graphics.Batch()
background = pyglet.graphics.OrderedGroup(0)
foreground = pyglet.graphics.OrderedGroup(1)

class ship(object):
    def __init__(self):
        ball_image = pyglet.image.load('ship.png')
        self.sprite = pyglet.sprite.Sprite(ball_image, x=50, y=50)
        self.key_handler = key.KeyStateHandler()
        self.bullets = []
        self.reload = 0
    def update(self, ts):
        global GFXbatch
        self.reload +=1
        if self.key_handler[key.LEFT]:
            self.sprite.x -= 4
        if self.key_handler[key.RIGHT]:
            self.sprite.x += 4
        if self.key_handler[key.X] and len(self.bullets)<10 and self.reload>10:
            self.bullets.append(bullet(GFXbatch, (self.sprite.x+25, self.sprite.y+25)))
            self.reload = 0

    def updateb(self, ts):
        for b in self.bullets:
            b.sprite.y += 8
            if b.sprite.y>480:
                self.removeBullet(b)
    def removeBullet(self, b):
        global GFXbatch
        self.bullets.remove(b)
        b.sprite.delete()

class bullet(object):
    def __init__(self, batch, pos):
        pattern = pyglet.image.SolidColorImagePattern((125, 0, 0, 255))
        image = pyglet.image.create(8, 8, pattern)
        self.sprite = pyglet.sprite.Sprite(img=image,x = pos[0], y = pos[1], batch=batch)

class target(object):
    def __init__(self, batch, pos):
        pattern = pyglet.image.SolidColorImagePattern((0, 125, 0, 255))
        image = pyglet.image.create(64, 64, pattern)
        self.sprite = pyglet.sprite.Sprite(img=image,x = pos[0], y = pos[1], batch=batch)

class Alien(object):
    def __init__(self, batch, pos):
        image =  pyglet.image.load('inv1.png')
        self.sprite = pyglet.sprite.Sprite(img=image,x = pos[0], y = pos[1], batch=batch)
        
    def update(self, ts):
        x = self.sprite.x
        y = self.sprite.y
        if x<500:
            x += 2
        else:
            x = 2
            y -= 5
            if y<80: y = 80
        self.sprite.x = x
        self.sprite.y = y
# Create text label
label = pyglet.text.Label('Shoot!', 
                          font_name='Arial', 
                          font_size=int(WinHeight/10),
                          x=window.width//2, y=window.height//2,
                          anchor_x='center', anchor_y='center',
                          batch=GFXbatch, group=foreground)
s = ship()
t = target(GFXbatch, (250, 216))
a = []
for i in range(6):
    a.append(Alien(GFXbatch, (10+(i*60), 430)))

window.push_handlers(s.key_handler)

# Move on the text along the path
def update(ts):
    global s
    s.update(ts)
    for si in a:
        si.update(ts)
def update2(ts):
    global s,t,a
    s.updateb(ts)
    
    oldbullets = []
    for b in s.bullets[:]:
        hits = [ alien for alien in a if spriteCollision(b.sprite, alien.sprite)]        
        #print hits
        #print a
        if len(hits)>0:
            oldbullets.append(b)
            for h in hits:
                h.sprite.delete()
                a.remove(h)
            s.removeBullet(b)
        #if spriteCollision(b.sprite, alien.sprite):
        #s.removeBullet(b)
        #alien.sprite.delete()
        #a.remove(alien)    
    return
    for b in s.bullets:
        if spriteCollision(b.sprite, t.sprite):
            t.sprite.y += 10
            if t.sprite.y>480: t.sprite.y=100
            s.removeBullet(b)

def spriteCollision(sprite1, sprite2):
    br = (sprite1.x, sprite1.y, sprite1.x + sprite1.width, sprite1.y + sprite1.height)
    tr = (sprite2.x, sprite2.y, sprite2.x + sprite2.width, sprite2.y + sprite2.height)
    return collision(br,tr)
    
def collision(rect1, rect2):
    if not(rect1[0]>rect2[2] or rect2[0]>rect1[2] or rect1[1]>rect2[3] or rect2[1]>rect1[3]):
        return True
    else:
        return False
        
pyglet.clock.schedule_interval(update,  1/100.0)
pyglet.clock.schedule_interval(update2,  1/200.0)

@window.event
def on_draw():
    global GFXbatch
    window.clear() 
    GFXbatch.draw()
    s.sprite.draw()
    
pyglet.app.run()

